package io.finer.erp.jeecg.bas.service.impl;

import io.finer.erp.jeecg.bas.entity.BasMaterialCategory;
import io.finer.erp.jeecg.bas.entity.ZrkDailyReportZj;
import io.finer.erp.jeecg.bas.entity.ZrkQzOrder;
import io.finer.erp.jeecg.bas.entity.ZzDevice;
import io.finer.erp.jeecg.bas.mapper.ZrkDailyReportZjMapper;
import io.finer.erp.jeecg.bas.service.IBasMaterialCategoryService;
import io.finer.erp.jeecg.bas.service.IZrkDailyReportZjService;
import io.finer.erp.jeecg.bas.service.IZrkQzOrderService;
import io.finer.erp.jeecg.bas.service.IZzDeviceService;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 制胶报表
 * @Author: jeecg-boot
 * @Date:   2021-08-12
 * @Version: V1.0
 */
@Service
public class ZrkDailyReportZjServiceImpl extends ServiceImpl<ZrkDailyReportZjMapper, ZrkDailyReportZj> implements IZrkDailyReportZjService {

	@Autowired
	private IZrkQzOrderService zrkQzOrderService;
	@Autowired
	private IZzDeviceService deviceService;
	@Autowired
	private ISysBaseAPI sysBaseAPI;
	@Autowired
	private IBasMaterialCategoryService categoryService;

	@Override
	public boolean updateReportZj(ZrkDailyReportZj zrkDailyReportZj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean saveReportZj(String id) {
		// TODO Auto-generated method stub
		ZrkQzOrder order = zrkQzOrderService.getById(id);
		//判断若是制胶生产完成，添加制胶日报表
		if(StringUtils.equals("1367753830647128066", order.getProcessId())) {
			ZrkDailyReportZj zj = new ZrkDailyReportZj();
			zj.setZjDate(order.getWoDeliverytime());
			if(oConvertUtils.isNotEmpty(order.getMachineId())) {
				ZzDevice device = deviceService.getById(order.getMachineId());
				if(device!=null) {
					zj.setMachineId(device.getDeviceName());
				}
			}
			if(oConvertUtils.isNotEmpty(order.getOperatorUsers())) {
				String[] userIds = order.getOperatorUsers().split(",");
				List<LoginUser> userList = sysBaseAPI.queryAllUserByIds(userIds);
				if(userList!=null && userList.size()>0) {
					StringBuffer users = new StringBuffer();
					for(LoginUser user:userList) {
						users.append(user.getRealname());
						users.append(",");
					}
					if(users!=null && users.length()>0) {
						zj.setOperatorUsers(users.toString().substring(0, users.toString().length()-1));
					}
				}
			}
			BasMaterialCategory category = categoryService.getById(order.getMaterialId());
			if(category!=null) {
				zj.setJsModel(category.getCode());
			}
			this.save(zj);
		}
		return false;
	}

}
